package com.gitee.huanminabc.fastboot.orm.hook;

import com.gitee.huanminabc.fastboot.context.AppContext;
import com.gitee.huanminabc.fastboot.load.hook.AppStartHook;
import com.gitee.huanminabc.fastboot.load.hook.AppStopHookLifecycleHook;
import com.gitee.huanminabc.fastboot.orm.aop.DbSwitchAop;
import com.gitee.huanminabc.fastboot.orm.aop.TransactionalAop;
import com.gitee.huanminabc.fastboot.web.context.AppWebContext;
import com.gitee.huanminabc.fastboot.web.servlet.HandlerInterceptor;
import com.gitee.huanminabc.jlambda_sql.jdbc.JdbcManage;

/**
 * 简要描述
 *
 * @Author: huanmin
 * @Date: 2025/4/13 01:01
 * @Version: 1.0
 * @Description: 文件作用详细描述....
 */
public class OrmAppStartLifecycleHook implements AppStartHook {
    @Override
    public void run() {
        AppContext.allAopInterceptorHandleClasses.add(DbSwitchAop.class);
        AppContext.allAopInterceptorHandleClasses.add(TransactionalAop.class);
        AppStopHookLifecycleHook.appStopHooks.add(new OrmAppStopHook());

        //如果是web项目, 那么需要在线程级别释放
        if (AppContext.moduleLoadMap.get("fast-boot-web")){
            //请求线程结束的时候释放数据库的连接和相关资源
            AppWebContext.handlerInterceptors.add(new HandlerInterceptor() {
                @Override
                public void afterCompletion() {
                    //线程结束后清理
                    JdbcManage.clear();
                }
            });
        }
    }
}
